using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace marbletrack
{/// <summary>
    /// Summary description for Class1
    /// </summary>
    class CurvePiece : TrackPiece
    {
        public CurvePiece()
        {
            // Setup variables to default values
            Vector3 rotation = Vector3.Zero;
            float scale = 1f;
            SetupVariables(rotation, scale);
        }

        public CurvePiece(Vector3 rotation, float scale)
        {
            // Setup Variables with specified values
            SetupVariables(rotation, scale);
        }

        // Setup Variables
        private void SetupVariables(Vector3 rotation, float scale)
        {
            // Setup orientation variables
            modelRotation_ = rotation;
            modelScale_ = scale;
            CorrectPosition_ = Matrix.CreateTranslation(-Game1.SQUARESIZE * 0.617f, -Game1.SQUARESIZE * 0.686f, Game1.SQUARESIZE * 0.05f);//Matrix.CreateTranslation(-Game1.SQUARESIZE * 0.75f, -Game1.SQUARESIZE * 0.336f, 0);
            // Load model
            model_ = Game1.CurveModel;

            // Create and Load physics actor
            trackActor_ = CreateMeshActor();
        }

        public override void SetEntryExitPoints()
        {
            float y = nextModelPosition_.Y;
            float x = nextModelPosition_.X;
            float z = nextModelPosition_.Z;

            if (modelRotation_ == directionFromWest)
            { x -= Game1.SQUARESIZE; }
            else if (modelRotation_ == directionFromNorth)
            { z -= Game1.SQUARESIZE; }
            else if (modelRotation_ == directionFromEast)
            { x += Game1.SQUARESIZE; }
            else if (modelRotation_ == directionFromSouth)
            { z += Game1.SQUARESIZE; }
            entryPoint_ = new Vector3(x, y + (Game1.HALFSQUARESIZE / 2), z);

            //y = nextModelPosition_.Y;
            x = nextModelPosition_.X;
            z = nextModelPosition_.Z;

            if (modelRotation_ == directionFromWest)
            { x += Game1.SQUARESIZE; }
            else if (modelRotation_ == directionFromNorth)
            { z += Game1.SQUARESIZE; }
            else if (modelRotation_ == directionFromEast)
            { x -= Game1.SQUARESIZE; }
            else if (modelRotation_ == directionFromSouth)
            { z -= Game1.SQUARESIZE; }
            exitPoint_ = new Vector3(x, y - (Game1.HALFSQUARESIZE / 2), z);
        }

        public override void SetNextModelPositionFromExitPoint(TrackPiece OtherModel)
        {
            nextModelPosition_ = OtherModel.exitPoint_;
            
            if (OtherModel.modelRotation_ == directionFromWest)
            { nextModelPosition_.X += Game1.HALFSQUARESIZE; }
            else if (OtherModel.modelRotation_ == directionFromNorth)
            { nextModelPosition_.Z += Game1.HALFSQUARESIZE; }
            else if (OtherModel.modelRotation_ == directionFromEast)
            { nextModelPosition_.X -= Game1.HALFSQUARESIZE; }
            else if (OtherModel.modelRotation_ == directionFromSouth)
            { nextModelPosition_.Z -= Game1.HALFSQUARESIZE; }
        }

        public static void FromStringList(List<string> savedStuff)
        {
            Game1.modelList.Add(new CurvePiece(
                new Vector3((float)Convert.ToDouble(savedStuff[1]),
                            (float)Convert.ToDouble(savedStuff[2]),
                            (float)Convert.ToDouble(savedStuff[3])),
                (float)Convert.ToDecimal(savedStuff[4])));
            ((CurvePiece)Game1.modelList[Game1.modelList.Count - 1]).nextModelPosition_.X = (float) Convert.ToDouble(savedStuff[5]);
            ((CurvePiece)Game1.modelList[Game1.modelList.Count - 1]).nextModelPosition_.Y = (float) Convert.ToDouble(savedStuff[6]);
            ((CurvePiece)Game1.modelList[Game1.modelList.Count - 1]).nextModelPosition_.Z = (float) Convert.ToDouble(savedStuff[7]);
        }
    }
}
